<?php
class User extends AppModel{
	var $primaryKey = 'id';
	var $hasMany = array('TweetFavorite'=>array('className'=>'TweetFavorite',
												'foreignKey'=>'user_id'),
	
						 'BlockUser'=>array('className'=>'BlockUser',
											'foreignKey'=>'user_id'),
	
						 'FollowUser'=>array('className'=>'FollowUser',
											 'foreignKey'=>'user_id'),
	
//						 'FollowUser'=>array('className'=>'FollowUser',
//											 'foreignKey'=>'user_id_followed'),
//						 'Reply'=>array( 'className'=>'Reply',
//										 'foreignKey'=>'user_id'),
	
						 'TweetBox'=>array( 'className'=>'TweetBox',
											'foreignKey'=>'user_id'),//);
	
						 'Tweet'=>array('className'=>'Tweet',
										'foreignKey'=>'user_id'));
	
	function login($username = null, $pass = null,$uid = null){
		$this->unbindModel(array('hasMany'=>array('TweetBox','Tweet','TweetFavorite','BlockUser','FollowUser')),false);
		$result = null;
		if(strstr($username,'@')){
			if($info = $this->findByEmailAndPassword($username,$pass)){
				$result = (array('is_success'=>true,'info'=>$info));
			}
		}else{
		if($info = $this->findByNickNameAndPassword($username,$pass)){
				$result = (array('is_success'=>true,'info'=>$info));
			}
		}
		if($result){
			App::uses('FollowUser','Model');
			App::uses('TweetFavorite','Model');
			App::uses('TweetBox','Model');
			App::uses('Tweet','Model');
			$followUser = new FollowUser();
			$tweetFavourite = new TweetFavorite();
			$tweet = new Tweet();
			$tbox = new TweetBox();
			//unbind model
			$followUser->unbindModel(array('belongsTo'=>array('User')),false);
			$tweetFavourite->unbindModel(array('belongsTo'=>array('User')),false);
			$tweet->unbindModel(array('belongsTo'=>array('User')),false);
			//fetch number
			$result['num_follower'] = $followUser->find('count',array('conditions'=>array('FollowUser.user_id_followed'=>$info['User']['id'])));
			$result['num_following'] = $followUser->find('count',array('conditions'=>array('FollowUser.user_id'=>$info['User']['id'])));
			$result['num_favourite'] = $tweetFavourite->find('count',array('conditions'=>array('TweetFavorite.user_id'=>$info['User']['id'])));
			$result['num_tweet'] = $tweet->find('count',array('conditions'=>array('Tweet.user_id'=>$info['User']['id'])))+$tbox->find('count',array('conditions'=>array('TweetBox.user_id'=>$info['User']['id'],'retweet'=>1)));
			//debug($result);	
			return json_encode($result);
			
		}else return json_encode(array('is_success'=>false,'message'=>'fail to connect'));
	}

	function getDetail($username = null,$uid = null){
		$this->unbindModel(array('hasMany'=>array('TweetBox','Tweet','TweetFavorite','BlockUser','FollowUser')),false);
		$result = null;
		if(strstr($username,'@')){
			if($info = $this->findByEmail($username)){
				$result = (array('info'=>$info));
			}
		}else{
			if($info = $this->findById($username)){
				$result = (array('info'=>$info));
			}
		}
		if($result){
			App::uses('FollowUser','Model');
			App::uses('TweetFavorite','Model');
			App::uses('TweetBox','Model');
			App::uses('Tweet','Model');
			$followUser = new FollowUser();
			$tweetFavourite = new TweetFavorite();
			$tweet = new Tweet();
			$tbox = new TweetBox();
			//unbind model
			$followUser->unbindModel(array('belongsTo'=>array('User')),false);
			$tweetFavourite->unbindModel(array('belongsTo'=>array('User')),false);
			$tweet->unbindModel(array('belongsTo'=>array('User')),false);
			//fetch number
			$result['num_follower'] = $followUser->find('count',array('conditions'=>array('FollowUser.user_id_followed'=>$info['User']['id'])));
			$result['num_following'] = $followUser->find('count',array('conditions'=>array('FollowUser.user_id'=>$info['User']['id'])));
			$result['num_favourite'] = $tweetFavourite->find('count',array('conditions'=>array('TweetFavorite.user_id'=>$info['User']['id'])));
			$result['num_tweet'] = $tweet->find('count',array('conditions'=>array('Tweet.user_id'=>$info['User']['id'])))+$tbox->find('count',array('conditions'=>array('TweetBox.user_id'=>$info['User']['id'],'retweet'=>1)));
			//debug($result);	
			return json_encode($result);
				
		}else return json_encode(array('is_success'=>false,'message'=>'fail to connect'));
	}
	
	
	function search($username = null, $uid = null,$index = null){
		$this->unbindModel(array('hasMany'=>array('TweetBox','Tweet','TweetFavorite','BlockUser','FollowUser')),false);
		//$result = null;
		if(strstr($username,'@')){
			if($info = $this->find('all',array('conditions'=>array('email LIKE'=>"%{$username}%"),
			'offset'=>($index-1)*30,
					'limit'=>30))){
				
			}
		}else{
			if($info = $this->find('all',array('conditions'=>array('nick_name LIKE'=>"%{$username}%"),
			'offset'=>($index-1)*30,
					'limit'=>30))){
				
			}
		}
		
 		if($info){
			App::uses('FollowUser','Model');
			$followUser = new FollowUser();
			//Unbind	
			$followUser->unbindModel(array('belongsTo'=>array('User')),false);
			// add isfollowing
			//debug($info);die;
			for($i = 0;$i<count($info);$i++){
				//debug($info[$i]);
				if( $followUser->findByUserIdAndUserIdFollowed($uid,$info[$i]['User']['id']))
				$info[$i]['User']['isfollowing'] = 1;
				else $info[$i]['User']['isfollowing'] = 0;
			}
			//debug($info);
			return json_encode($info);
				
		}else return json_encode(array('is_success'=>false,'message'=>'can not find'));
	}
}